package com.oocl.bookproducer;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import pojo.Book;

public class PtpProducer {
	private BookFileReader bfr;
	private List<String> booksList = null;
	private ConnectionFactory factory;
	private Destination queue;
	private Connection conn;
	private Session sen;
	private MessageProducer producer;
	private TextMessage msg;
	
	public PtpProducer(String filename) throws Exception{
		this.bfr=new BookFileReader(filename);
		this.booksList=this.bfr.getBookslist();
	}
	
	public void produce() throws Exception{
		factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		queue = new ActiveMQQueue("ZivTest");
		conn = factory.createConnection();
		conn.start();
		
		sen = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		producer = sen.createProducer(queue);
		for (int i = 0; i < booksList.size(); i++) {
			msg = sen.createTextMessage(booksList.get(i));
			producer.send(msg);
		}
		producer.close();
		sen.close();
		conn.close();
	}
	
	public static void main(String[] args) throws Exception {
		new PtpProducer("json.txt").produce();
	}
}
